package com.tank.model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import com.tank.common.Dir;
import com.tank.config.ResourceMgr;
import com.tank.config.TankConfig;
import com.tank.util.ImageUtils;

/**
 * @author chenwb
 * @since 2024/1/21 14:11
 */
public class Tank implements Liver {
    // 坦克的位置
    private int x;
    private int y;
    // 坦克的方向
    private Dir dir;
    /**
     * 坦克的速度
     */
    private int speed;
    /**
     * 图片
     */
    private final BufferedImage bufferedImage;
    private int killCount = 0;
    /**
     * 坦克是否存活
     */
    private boolean isAlive = true;
    private boolean isMoving = false;
    private final List<Bullet> bullets = new ArrayList<>();

    private Rectangle rectangle;
    /**
     * 默认图片
     */
    private static final BufferedImage DEFAULT_BUFFERED_IMAGE = ResourceMgr.getTankImage(TankConfig.DEFAULT_TANK_DIR);

    public Tank(int x, int y) {
        this(x, y, TankConfig.DEFAULT_TANK_DIR);
    }

    public Tank(int x, int y, Dir dir) {
        this(x, y, dir, TankConfig.DEFAULT_TANK_SPEED);
    }

    public Tank(int x, int y, Dir dir, int speed) {
        this(x, y, dir, speed, DEFAULT_BUFFERED_IMAGE);
    }

    public Tank(int x, int y, Dir dir, BufferedImage bufferedImage) {
        this(x, y, dir, TankConfig.DEFAULT_TANK_SPEED, bufferedImage);
    }

    public Tank(int x, int y, Dir dir, int speed, BufferedImage bufferedImage) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.speed = speed;
        this.bufferedImage = bufferedImage;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getKillCount() {
        return killCount;
    }

    public void addKillCount() {
        this.killCount++;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public Dir getDir() {
        return this.dir;
    }

    public void setMoving(boolean moving) {
        isMoving = moving;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public List<Bullet> getBullets() {
        return bullets;
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        // 根据方向画出坦克
        // g.drawRect(x, y, bufferedImage.getWidth(), bufferedImage.getHeight());
        g.drawImage(ImageUtils.getImageByDir(bufferedImage, dir), x, y, null);

        if (isMoving) {
            move();
        }
    }

    public void move() {
        switch (dir) {
            case UP:
                y -= this.speed;
                break;
            case DOWN:
                y += this.speed;
                break;
            case LEFT:
                x -= this.speed;
                break;
            case RIGHT:
                x += this.speed;
                break;
            default:
                break;
        }
    }

    public void fire() {
        Bullet bullet = new Bullet(getBulletX(), getBulletY(), getDir());
        bullets.add(bullet);
    }

    private int getBulletX() {
        switch (dir) {
            case UP:
                return x + this.bufferedImage.getWidth() / 2;
            case DOWN:
                return x + this.bufferedImage.getWidth() / 2;
            case LEFT:
                return x;
            case RIGHT:
                return x + this.bufferedImage.getWidth();
        }
        return x;
    }

    private int getBulletY() {
        switch (dir) {
            case UP:
                return y;
            case DOWN:
                return y + this.bufferedImage.getHeight();
            case LEFT:
                return y + this.bufferedImage.getHeight() / 2;
            case RIGHT:
                return y + this.bufferedImage.getHeight() / 2;
        }
        return y;
    }

    @Override
    public String toString() {
        return "Tank{" + "x=" + x + ", y=" + y + ", dir=" + dir + ", speed=" + speed + ", imageIcon=" + bufferedImage
            + ", isAlive=" + isAlive + ", isMoving=" + isMoving + ", bullets=" + bullets + ", DEFAULT_IMAGE_ICON="
            + DEFAULT_BUFFERED_IMAGE + '}';
    }

    @Override
    public void die() {
        this.isAlive = false;
    }

    @Override
    public boolean isAlive() {
        return !this.isAlive;
    }

    @Override
    public Rectangle getRectangle() {
        if (rectangle != null) {
            return rectangle;
        }
        return new Rectangle(x, y, bufferedImage.getWidth(), bufferedImage.getHeight());
    }
}
